# exerc2.py
# 主模块
def main():
    # 接收用户输入并验证
    input_str = input("请输入一个字符串(0-255个字符): ")
    if len(input_str) == 0 or len(input_str) > 255:
        print("输入字符串长度必须在1-255之间")
        return

    # 调用letter模块处理
    english_chars = filter_english_letters(input_str)
    print("过滤后的英文字符:", english_chars)

    # 调用reverse模块处理
    reversed_chars = reverse_string(english_chars)
    print("反转后的字符串:", reversed_chars)

    # 调用enCode模块处理
    encrypted_str = caesar_cipher(reversed_chars, shift=3)  # 使用凯撒密码，秘钥设为3
    print("加密后的结果:", encrypted_str)


# letter模块：过滤非英文字符
def filter_english_letters(input_str):
    result = []
    for char in input_str:
        if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
            result.append(char)
    return ''.join(result)


# reverse模块：反转字符串
def reverse_string(input_str):
    reversed_chars = []
    length = len(input_str)
    for i in range(length - 1, -1, -1):  # 从后向前遍历
        reversed_chars.append(input_str[i])
    return ''.join(reversed_chars)


# enCode模块：凯撒密码加密
def caesar_cipher(text, shift=3):
    result = []
    for char in text:
        if 'a' <= char <= 'z':
            shifted = ord(char) - ord('a')
            shifted = (shifted + shift) % 26
            result.append(chr(shifted + ord('a')))
        elif 'A' <= char <= 'Z':
            shifted = ord(char) - ord('A')
            shifted = (shifted + shift) % 26
            result.append(chr(shifted + ord('A')))
        else:
            result.append(char)
    return ''.join(result)


if __name__ == '__main__':
    main()